#ifndef TCPACCEPTOR_H
#define TCPACCEPTOR_H

#include "network_global.h"
#include <boost/noncopyable.hpp>
#include <boost/asio.hpp>
#include <boost/function.hpp>

class TcpConnection;

class NETWORK_EXPORT TcpAcceptor :
        protected boost::noncopyable
{
public:
    typedef boost::asio::io_service Service;
    typedef boost::asio::ip::tcp::acceptor Acceptor;
    typedef boost::asio::ip::tcp::socket Socket;
    typedef boost::asio::ip::tcp::endpoint Endpoint;
    typedef boost::asio::ip::address Address;
    typedef boost::system::error_code ErrorCode;

    typedef boost::function< void (int) > Accepter;

public:
    TcpAcceptor(Service &service);
    ~TcpAcceptor();

    int open(const std::string &host, unsigned short port, int listenCount);

    void asyncAccept(Socket &sock,
                     BOOST_ASIO_MOVE_ARG(Accepter) a);

    void close();

protected:
    void onAccept(Accepter a, ErrorCode ec);

private:
    Acceptor m_acceptor;
};

#endif // TCPACCEPTOR_H
